#!/usr/bin/env python
import os
import sys
import django

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'devops_backend.settings')
django.setup()

from rbac.models import Permission, Role, Menu, User

def create_submenus():
    """创建子菜单"""
    # 获取父菜单
    system_menu = Menu.objects.get(path='/system')
    monitor_menu = Menu.objects.get(path='/monitor')
    database_menu = Menu.objects.get(path='/database')
    
    # 获取权限
    permissions = {p.code: p for p in Permission.objects.all()}
    
    # 创建系统管理子菜单
    submenus = [
        {
            'parent': system_menu,
            'name': '用户管理',
            'path': '/system/users',
            'component': 'system/UserManagement',
            'icon': 'el-icon-user',
            'sort_order': 1,
            'permission_codes': ['user_manage']
        },
        {
            'parent': system_menu,
            'name': '角色管理',
            'path': '/system/roles',
            'component': 'system/RoleManagement',
            'icon': 'el-icon-s-custom',
            'sort_order': 2,
            'permission_codes': ['role_manage']
        },
        {
            'parent': system_menu,
            'name': '权限管理',
            'path': '/system/permissions',
            'component': 'system/PermissionManagement',
            'icon': 'el-icon-key',
            'sort_order': 3,
            'permission_codes': ['permission_manage']
        },
        {
            'parent': system_menu,
            'name': '菜单管理',
            'path': '/system/menus',
            'component': 'system/MenuManagement',
            'icon': 'el-icon-menu',
            'sort_order': 4,
            'permission_codes': ['menu_manage']
        },
        # 运维监控子菜单
        {
            'parent': monitor_menu,
            'name': '系统监控',
            'path': '/monitor/system',
            'component': 'monitor/SystemMonitor',
            'icon': 'el-icon-cpu',
            'sort_order': 1,
            'permission_codes': ['system_monitor']
        },
        {
            'parent': monitor_menu,
            'name': '日志管理',
            'path': '/monitor/logs',
            'component': 'monitor/LogManagement',
            'icon': 'el-icon-document',
            'sort_order': 2,
            'permission_codes': ['log_view']
        },
        # 数据库管理子菜单
        {
            'parent': database_menu,
            'name': '数据库概览',
            'path': '/database/overview',
            'component': 'database/DatabaseManagement',
            'icon': 'el-icon-data-board',
            'sort_order': 1,
            'permission_codes': ['database_manage']
        },
        {
            'parent': database_menu,
            'name': '连接管理',
            'path': '/database/connections',
            'component': 'database/ConnectionManagement',
            'icon': 'el-icon-connection',
            'sort_order': 2,
            'permission_codes': ['database_connection_manage']
        },
        {
            'parent': database_menu,
            'name': 'SQL查询',
            'path': '/database/query',
            'component': 'database/QueryTool',
            'icon': 'el-icon-edit-pen',
            'sort_order': 3,
            'permission_codes': ['database_query']
        },
        {
            'parent': database_menu,
            'name': '备份管理',
            'path': '/database/backup',
            'component': 'database/BackupManagement',
            'icon': 'el-icon-download',
            'sort_order': 4,
            'permission_codes': ['database_backup']
        },
        {
            'parent': database_menu,
            'name': '性能监控',
            'path': '/database/monitoring',
            'component': 'database/MonitoringDashboard',
            'icon': 'el-icon-monitor',
            'sort_order': 5,
            'permission_codes': ['database_monitor']
        },
    ]
    
    for submenu_data in submenus:
        menu, created = Menu.objects.get_or_create(
            path=submenu_data['path'],
            defaults={
                'name': submenu_data['name'],
                'parent': submenu_data['parent'],
                'component': submenu_data['component'],
                'icon': submenu_data['icon'],
                'sort_order': submenu_data['sort_order'],
                'is_active': True
            }
        )
        if created:
            # 设置权限
            menu_permissions = [permissions[code] for code in submenu_data['permission_codes'] if code in permissions]
            menu.permissions.set(menu_permissions)
            print(f"创建子菜单: {menu.name}")
        else:
            print(f"子菜单已存在: {menu.name}")

if __name__ == '__main__':
    create_submenus()
    print("子菜单创建完成！")
